package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.mapper.Pms_Sku_Sale_Value_Mapper;
import com.shop.pojo.pms.Pms_Attr;
import com.shop.pojo.pms.Pms_Sku_Sale_Attr_Value;
import com.shop.service.Pms_Attr_Service;
import com.shop.service.Pms_Sku_Sale_Value_Service;
import com.shop.utils.R;
import com.shop.vo.Attr_values_checked;
import com.shop.vo.SaleAttrByValues;
import com.shop.vo.SkuItemSaleAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class Pms_Sku_Sale_Value_ServiceImpl extends ServiceImpl<Pms_Sku_Sale_Value_Mapper, Pms_Sku_Sale_Attr_Value> implements Pms_Sku_Sale_Value_Service {
    @Autowired
    private Pms_Sku_Sale_Value_Mapper pms_sku_sale_value_mapper;
    @Autowired
    private Pms_Attr_Service pms_attr_service;
    //查询spu下的全部销售属性
    @Override
    public List<SkuItemSaleAttrVo> getSaleAttrsBySpuId(Long spuId) {
        return pms_sku_sale_value_mapper.getSaleAttrsBySpuId(spuId);
    }

    /*
     * @Description 根据分类来查询当前分类下的全部销售属性
     * 然后拿到全部的属性id封装成list
     * 拿这个封装好的属性id的集合去sku销售属性值里去查找数据
     * 然后通过stream流对刚开始分类下的全部销售属性进行流
     * 然后在流里面将属性名和id赋值给我们的SaleAttrByValues模型里面
     * 并且在这个里面我们需要遍历sku销售属性值
     * 在里面查找销售属性的value，然后通过spilt来进行分割，最后给list集合
     * 最后将这个存有list集合的属性值在复制给我们的SaleAttrByValues模型里面的values
     * 最终return出去
     * 目的：查询全部销售属性名、这个属性名有值的话则去查找并且赋值，没有值的话就拉倒
     * @param catalog_id
     * @return java.util.List<com.shop.vo.SaleAttrByValues>
     * @author liusupeng
     * @date 2020/11/2 19:54
     */
    @Override
    public R getSkuSaleValueByCatelogId(Long catalog_id) {
        R r1=new R();
        List<Pms_Attr> allSaleAttr = pms_attr_service.findAllSaleAttr(catalog_id);
        //全部销售属性
        List<Long> saleAttrId = allSaleAttr.stream().map((x) -> {
            Long attr_id = x.getAttr_id();
            return attr_id;
        }).collect(Collectors.toList());

        QueryWrapper<Pms_Sku_Sale_Attr_Value> queryWrapper=new QueryWrapper<Pms_Sku_Sale_Attr_Value>();
        queryWrapper.in("attr_id",saleAttrId).groupBy("attr_name");

        List<Pms_Sku_Sale_Attr_Value> skuSaleValueByCatelogId = pms_sku_sale_value_mapper.getSkuSaleValueByCatelogId(queryWrapper);

        List<SaleAttrByValues> SaleAttrByValues = allSaleAttr.stream().map((x) -> {
            SaleAttrByValues saleAttrByValues = new SaleAttrByValues();
            saleAttrByValues.setAttr_id(x.getAttr_id());
            saleAttrByValues.setAttr_name(x.getAttr_name());
            skuSaleValueByCatelogId.forEach(y->{
                List<Attr_values_checked> stringList = new ArrayList<Attr_values_checked>();
                if(x.getAttr_name().equals(y.getAttr_name())){
                    String[] split = y.getAttr_value().split(",");

                    for (String s : split) {
                        Attr_values_checked attr_values_checked=new Attr_values_checked();
                        attr_values_checked.setValues(s);
                        stringList.add(attr_values_checked);
                    }
                    saleAttrByValues.setAttr_values(stringList);
                }
            });

            return saleAttrByValues;
        }).collect(Collectors.toList());



        //  System.out.println(skuSaleValueByCatelogId);
      /*
       * @Description
       * @param catalog_id
       * @return com.shop.utils.R
       * @author liusupeng
       * @date 2020/11/2 21:46
       *   List<SaleAttrByValues> collect = skuSaleValueByCatelogId.stream().map((x) -> {
            SaleAttrByValues saleAttrByValues = new SaleAttrByValues();
            String[] split = x.getAttr_value().split(",");
            List<String> stringList = new ArrayList<>();
            for (String s : split) {
                stringList.add(s);
            }
            saleAttrByValues.setAttr_id(x.getAttr_id());
            saleAttrByValues.setAttr_name(x.getAttr_name());
            saleAttrByValues.setAttr_values(stringList);

            return saleAttrByValues;
        }).collect(Collectors.toList());
       */
        r1.data(SaleAttrByValues);
        return r1;
    }



    /**
     * 根据商品的skuID查询商品的属性&值
     * @param skuId
     * @return
     */
    @Override
    public List<String> get_web_Sku_Sale_Values(Long skuId) {
        return pms_sku_sale_value_mapper.get_web_Sku_Sale_Values(skuId);
    }
}
